package com.leetcode.LC_MED;

import java.util.Arrays;

public class MST17_10 {
    public int majorityElement(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        Arrays.sort(nums);
        int count = 1;
        for (int i = 0; i < nums.length; i++) {
            if (i + 1 < nums.length && nums[i] == nums[i + 1]) {
                count++;
                if (count > nums.length / 2) {
                    return nums[i];
                }
            } else {
                count = 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new MST17_10().majorityElement(new int[]{2, 2}));
    }
}
